package com.cloud.controller;

import com.cloud.entity.Employee;
import com.cloud.entity.Order;
import com.cloud.entity.PageInfo;
import com.cloud.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author .
 * created in 2020/1/18 9:34
 */

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    private OrderService orderServiceImpl;

    public OrderController(OrderService orderServiceImpl) {
        this.orderServiceImpl = orderServiceImpl;
    }

    @PostMapping(value = "/addOrder")
    public int addOrder(Order order, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("emp");
        order.setCreater(emp.getName());
        order.setUpdater(emp.getName());
        return orderServiceImpl.addOrder(order);
    }

    @PostMapping(value = "/delOrder/{id}")
    public int delOrder(@PathVariable("id") int id) {
        return orderServiceImpl.delOrder(id);
    }

    @PostMapping(value = "/updateOrder")
    public int updateOrder(Order order, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("emp");
        order.setUpdater(emp.getName());
        return orderServiceImpl.updateOrder(order);
    }

    @GetMapping(value = "/findAllOrdersByPage")
    public PageInfo<Order> findAllOrdersByPage(PageInfo<Order> info,
                                               @RequestParam(value = "no", defaultValue = "") String no) {
        return orderServiceImpl.findAllOrdersByPage(info, no);
    }

    @GetMapping(value = "/findById/{id}")
    public Order findById(@PathVariable("id") int id) {
        return orderServiceImpl.findById(id);
    }

}
